﻿
layui.use(['upload', 'table', 'element'], function () {
    upload = layui.upload;
    element = layui.element;
    var table = layui.table;

    var uploadListIns = upload.render({
        elem: '#choicethefiles'
        , elemList: $('#demoListtijian') //列表元素对象
        , url: '/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        , accept: 'file'
        , multiple: true
        , number: 3
        , auto: false
        , bindAction: '#uploadthetijian'
        , choose: function (obj) {
            $("#uploadupload").show();
            var that = this;
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件

            console.log("完成文件的选择");
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td><input style="border: 0;" readonly class="healthname" type="text" value="'
                    + file.name + '"/></td>'
                    , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                    , '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                    , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                that.elemList.append(tr);
                element.render('progress'); //渲染新加的进度条组件
            });
        }
        , done: function (res, index, upload) { //成功的回调

            console.log("成功啦");
            var that = this;
            //if(res.code == 0){ //上传成功
            var tr = that.elemList.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(3).html('');

            var tags = [];
            $(".healthname").each(function (i, e) {

                tags[i] = $(this).val();


            })

            $("#healthfile").val(tags.toString());

            
            //document.getElementById('healthfile').value = ;
            //清空操作
            delete this.files[index]; //删除文件队列已经上传成功的文件
            return;
            //}
            this.error(index, upload);
        }
        , allDone: function (obj) { //多文件上传完毕后的状态回调
            console.log(obj);
            console.log($("#healthfile").val());
            insertintoFiles($("#healthfile").val(), $("#jbxxid").val());
            $("#uploadupload").hide();
            table.reload('filestable', {

                where: {

                    "Jbxxid": $("#jbxxid").val()


                }

            });


        }
        , error: function (index, upload) { //错误回调
            console.log("错啦~");
            var that = this;
            var tr = that.elemList.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传

            var tags = [];
            $(".healthname").each(function (i, e) {

                tags[i] = $(this).val();


            })

            $("#healthfile").val(tags.toString());


        }
        , progress: function (n, elem, e, index) { //注意：index 参数为 layui 2.6.6 新增
            element.progress('progress-demo-' + index, n + '%'); //执行进度条。n 即为返回的进度百分比
        }
    });




});

function insertintoFiles(string,uuid) {

    if ($("#jbxxid").val() == "") {
        layer.msg("请先填写了基本信息了多");
        return;
    }



    var x = string;
    $.ajax({
        url: "/Infos/_Putfiles",
        type: "post",
        async: false,
        data: {"cpFile": x,"uuid":uuid},
 /*       data: {"s": x,"uuid":uuid},*/
        success: function (datas) {

            console.log("成功上传啦");
            //$("#gangweisave").attr("disabled", true);
            //document.getElementById("gangweisave").innerHTML = "已经完成保存，请勿重复提交";
            //lis = datas;
            //console.log(lis);
            //globalparams = datas;
            layui.use('layer', function () {
                layer = layui.layer;
                layer.msg('该文件完成上传', { icon: 1 }
                );
            });





        }
    })








}


function getthejiaoyushitinfo() {



    console.log("正在获取文件!!!!");

    var expreess = $("#jbxxid").val() === '' ? '' : '?Jbxxid=' + $("#jbxxid").val();

    return "/infos/Getthejiaoyuininfos" + expreess;



}
function getthesituationsinfo() {



    console.log("正在获取文件!!!!");

    var expreess = $("#jbxxid").val() === '' ? '' : '?Jbxxid=' + $("#jbxxid").val();

    return "/infos/GettheSituationsInfo" + expreess;
   


}
layui.use('table', function () {
    var table = layui.table;
    console.log("kk7kk");
    console.log($("#jbxxid").val());

    table.render({
        elem: '#filestable',
        //url:'/infos/GetJobinfosbyList?Jbxxid='+$("#jbxxid").val()
        url: getthesituationsinfo(),
        toolbar: '' //开启头部工具栏，并为其绑定左侧模板
        , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
            title: '提示'
            , layEvent: 'LAYTABLE_TIPS'
            , icon: 'layui-icon-tips'
        }]




        , title: '体检报告浏览'
        , cols: [[
            /* { type: 'checkbox', fixed: 'left' }*/

            { field: 'cpFile', title: '文件名', width: 130, fixed: 'left', templet: prooffiles},

            { fixed: 'right', title: '操作', width: 125, minWidth: 125, toolbar: '#deletethefiles' }

             
        ]]
        , page: true
    });


    table.on('tool(filestable)', function (obj) {
        var data = obj.data;
        console.log(data);
        if (obj.event === 'del_oldht') {


            layer.confirm('请问您真的想删除改条附件？？',
                //layer.confirm('请问您真的想删除这行数据？',
                function (index) {


                    //console.log(data.hasOwnProperty('diseasehistory'));

                    //deletethefamilys(data.id);

                    deletethefiles(data.id);
                    table.reload('filestable', {

                        where: {

                            "Jbxxid": $("#jbxxid").val()


                        }

                    });


                    layer.close(index);
                });
        }


        if (obj.event === 'clicktheshit') {


            layer.msg("sssssss");
        }
    });

    //table.on('tool(filestable)', function (obj) {
    //    var data = obj.data;//获取监听点击当前行的所有信息[object，object]
    //    console.log(data);
    //    var url = "/publish/upload/" + data.cpFile;
    //    //var url = "http://www.baidu.com";
    //    window.open(url);
    //});

    //table.on('tool(filestable)', function (obj) {
    //    var data = obj.data;
    //    if (obj.event === 'clicktheshit') {

    //        layer.msg("haola~~~!!");


    //    }
    //});








});



function deletethefiles(id) {
    $.ajax({
        url: "/Infos/deletethefiles",
        type: "post",
        async: false,
        data: {

            _id: id
        },
        success: function (datas) {



            //lis = datas;
            //console.log(lis);

            layui.use('layer', function () {
                layer = layui.layer;
                layer.msg('该条数据完成删除', { icon: 1 }
                );
            });

        }
    }
    );
}

function deletethejiaoyu(id) {
    $.ajax({
        url: "/Infos/deletethejiaoyuxinxi",
        type: "post",
        async: false,
        data: {

            _id: id
        },
        success: function (datas) {



            //lis = datas;
            //console.log(lis);

            layui.use('layer', function () {
                layer = layui.layer;
                layer.msg('该条数据完成删除', { icon: 1 }
                );
            });

        }
    }
    );
}


layui.use('table', function () {
    var table = layui.table;

    //执行一个 table 实例
    table.render({
        elem: '#educationfiles'
        //, url: '/infos/GeteducationsInfosbyList?Jbxxid='+$("#jbxxid").val()
        , url: getthejiaoyushitinfo()
        , method: "get"
        , contentType: 'json'

        , even: true
        //, cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        //, title: '用户表'
        , page: true //开启分页
         //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , cols: [[
            { field: 'cpFile', width: 180, title: '附件名称', templet: prooffiles }

            //,{field:'fileUrl', width:80, title:'附件'},
            , { fixed: 'right', title: '操作', width: 125, minWidth: 125, toolbar: '#deletethefiles' }
        ]]

    });
    table.on('tool(educationfiles)', function (obj) {
        var data = obj.data;
        //console.log(data);
        if (obj.event === 'del_oldht') {


            layer.confirm('请问您真的想删除改条附件？？',
                //layer.confirm('请问您真的想删除这行数据？',
                function (index) {


                    //console.log(data.hasOwnProperty('diseasehistory'));

                    //deletethefamilys(data.id);

                    deletethejiaoyu(data.id);
                    table.reload('educationfiles', {

                        where: {

                            "Jbxxid": $("#jbxxid").val()


                        }

                    });


                    layer.close(index);
                });



        }



    })


});



